package com.gitee.feizns.quickstart.easyexcel.style;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.List;

/**
 * 风格
 * @author feizns
 * @since 2025/4/21
 */
public class Styles {

    /**
     * 自定义列宽样式策略
     * @param width 宽度
     * @return {@link AbstractColumnWidthStyleStrategy}
     */
    public static AbstractColumnWidthStyleStrategy setWidth(int width) {
        return new AbstractColumnWidthStyleStrategy() {
            @Override
            protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
                //简单设置
                Sheet sheet = writeSheetHolder.getSheet();
                sheet.setColumnWidth(cell.getColumnIndex(), width);
            }

        };
    }

    /**
     * 默认单元格样式
     * @return {@link HorizontalCellStyleStrategy }
     */
    public static HorizontalCellStyleStrategy defaultHorizontalCellStyleStrategy() {
        return new HorizontalCellStyleStrategy(defaultHeadCellStyle(), defaultContextCellStyle());
    }

    /**
     * 默认上下文单元格样式
     * @return {@link WriteCellStyle }
     */
    public static WriteCellStyle defaultContextCellStyle() {
        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        WriteFont font = font("微软雅黑");
        font.setColor(IndexedColors.BLACK.getIndex());
        cellStyle.setWriteFont(font);
        return cellStyle;
    }

    /**
     * 默认头像样式
     * @return {@link WriteCellStyle }
     */
    public static WriteCellStyle defaultHeadCellStyle() {
        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.SEA_GREEN.index);
        cellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBottomBorderColor(IndexedColors.WHITE1.index);
        cellStyle.setTopBorderColor(IndexedColors.WHITE1.index);
        cellStyle.setLeftBorderColor(IndexedColors.WHITE1.index);
        cellStyle.setRightBorderColor(IndexedColors.WHITE1.index);
        cellStyle.setWrapped(true);
        //文字
        WriteFont font = font("微软雅黑");
        font.setFontHeightInPoints((short) 11);
        font.setColor(IndexedColors.WHITE.getIndex());
        font.setBold(true);
        cellStyle.setWriteFont(font);
        return cellStyle;
    }

    /**
     * 字体
     * @param fontName 字体名称
     * @return {@link WriteFont }
     */
    public static WriteFont font(String fontName) {
        WriteFont font = new WriteFont();
        font.setFontName(fontName);
        return font;
    }

}
